// import * as path from "path";

import SceneGame, { EItemType } from "./SceneGame";

export default class GameUtil {
    private static _instance: GameUtil = null;
    public static get instance() {
        if (this._instance === null) {
            this._instance = new this();
        }
        return this._instance;
    }

    public static isTest: boolean = true;

    public static sleep(ms: number): Promise<void> {
        return new Promise<any>(function (resolve, reject) {
            setTimeout(function () {
                resolve(`GameUtil.sleep()`);
            }, ms);
        });
    }

    // 交集
    public intersect(a, b) {
        return a.filter(function (v) { return b.indexOf(v) > -1 });
    }

    // 差集
    public minus(a, b) {
        return a.filter(function (v) { return b.indexOf(v) == -1 });
    }

    // 补集
    public complement(a, b) {
        return a.filter(function (v) { return !(b.indexOf(v) > -1) })
            .concat(b.filter(function (v) { return !(a.indexOf(v) > -1) }));
    }

    // 并集
    public union(a, b) {
        return a.concat(b.filter(function (v) { return !(a.indexOf(v) > -1) }));
    }

    public getGuestSkeData() {
        const arr = [
            { name: 'baoxiaohai', y: -360 },
            { name: 'chaonan', y: -140 },
            { name: 'chaonv', y: -140 },
            { name: 'dama', y: -330 },
            { name: 'daye', y: -380 },
            { name: 'shudaizi', y: -130 },
            { name: 'youdiyuan', y: -310 },
        ];

        const guestData = SceneGame.instance.getGuestDatas();
        for (let i = arr.length - 1; i > -1; i--) {
            for (let k = 0; k < guestData.length; k++) {
                const data = guestData[k];
                if (data.ske.name === arr[i].name) {
                    arr.splice(i, 1);
                    break;
                }
            }
        }

        const name = arr[Math.floor(Math.random() * arr.length)];
        return name;
    }

    public getColorByState(isUnlock: boolean) {
        return isUnlock ? '#FFFFFF' : '#D0CFCF';
    }

    /**
     * 动态加载item资源
     * @param type 来源
     * @param itemId 
     * @param sprite 
     * @param maxWidth 
     * @param maxHeight 
     * @param cb 
     * @returns 
     */
    public setItemIcon(type: EItemType, itemId: number, sprite: cc.Sprite, maxWidth: number, maxHeight: number, cb?: Function) {
        sprite.spriteFrame = null;
        if (itemId < 0 || itemId == null) {
            return;
        }

        const realId = SceneGame.instance.data.ids[itemId];
        const url = type !== EItemType.Test ? `texture/item/item${realId}` : `texture/item/item${itemId + 1}`;
        if (type !== EItemType.Test && realId === undefined) {
            console.log("undefined:", SceneGame.instance.data.ids, realId, itemId);
        }
        cc.resources.load(url, cc.SpriteFrame, (err, spriteFrame) => {
            if (err) {
                console.error(err);
                return;
            }

            sprite.spriteFrame = spriteFrame;
            sprite.node.width = spriteFrame.getRect().width;
            sprite.node.height = spriteFrame.getRect().height;

            const sx = maxWidth / sprite.node.width;
            const sy = maxHeight / sprite.node.height;
            sprite.node.scale = sx > sy ? sy : sx;
            // console.log(sx, sy, sprite.node.scale, sprite.node.width * sprite.node.scale, sprite.node.height * sprite.node.scale);

            cb && cb();
        });
    }
}